//-------------------------------------------------------------------
//	Copyright (c) 2012-2014 Zhirnov Andrey
//	This file is part of the "UXGEN-ES" project.
//	See copyright notice in "Engine.h".
//-------------------------------------------------------------------

#pragma once

#include "Platforms/File/FileSystem.h"

#if defined( PLATFORM_ANDROID )

namespace Engine
{
namespace Platforms
{

	//
	// Resource File
	//

	class ResourceFile : public File::RFile
	{
	// variables
	protected:
		Ptr< android::AAsset >	_asset;
		string					_name;


	// methods
	public:
		ResourceFile (android::AAsset * asset, uni_c_string fname) :
			_asset(asset), _name(fname)
		{}

		~ResourceFile ()	{ _Close(); }


		// RFile //
		virtual isize ReadBuf (void * buf, usize size) override
		{
			return android::AAsset_read( _asset, buf, size );
		}


		// BaseFile //
		virtual void Close () override
		{
			return _Close();
		}

		virtual bool IsOpened () override
		{
			return _asset.IsNotNull();
		}

		virtual bool SeekSet (usize offset) override
		{
			return android::AAsset_seek( _asset, offset, SEEK_SET ) != -1;
		}

		virtual bool SeekCur (isize offset) override
		{
			return android::AAsset_seek( _asset, offset, SEEK_CUR ) != -1;
		}

		virtual bool SeekEnd (usize offset) override
		{
			return android::AAsset_seek( _asset, offset, SEEK_END ) != -1;
		}
		
		virtual usize RemainingSize () const override
		{
			return android::AAsset_getRemainingLength( _asset );
		}

		virtual usize Size () const override
		{
			return android::AAsset_getLength( _asset );
		}

		virtual usize Pos () const override
		{
			return android::AAsset_getLength( _asset ) - android::AAsset_getRemainingLength( _asset );
		}
		
		virtual usize IsEOF () const override
		{
			return Pos() >= Size();
		}

		virtual uni_c_string	Name () const override
		{
			return _name;
		}

		virtual File::EFile::type		GetType () const override
		{
			return File::EFile::RESOURCE;
		}


	private:
		void _Close ()
		{
			if ( IsOpened() )
			{
				android::AAsset_close( _asset );
				_asset = null;
			}
			_name.Clear();
		}
	};



	//
	// File System
	//

	class AndroidFileSystem : public IFileSystem
	{
	// methods
	public:
		AndroidFileSystem (EngineSubSystemsRef ess) : IFileSystem(ess) {}
	};

	

//------------------------ IFileSystem -------------------------//
/*
=================================================
	GetFreeSpace
----
	returns free space on disc (path)
=================================================
*/
	inline uint64 IFileSystem::GetFreeSpace (uni_c_string path) const
	{
		using namespace android;

		struct statfs fiData;

		if ( statfs( path.cstr(), &fiData ) >= 0 )
		{
			return uint64(fiData.f_bfree) * fiData.f_bsize;
		}
		return MaxValue<uint64>();
	}
	
/*
=================================================
	GetTotalSpace
----
	returns total space on disc (path)
=================================================
*/
	inline uint64 IFileSystem::GetTotalSpace (uni_c_string path) const
	{
		using namespace android;

		struct statfs fiData;

		if ( statfs( path.cstr(), &fiData ) >= 0 )
		{
			return uint64(fiData.f_blocks) * fiData.f_bsize;
		}
		return MaxValue<uint64>();
	}


}	// Platforms
}	// Engine

#endif	// PLATFORM_ANDROID